package dream_scene

import (
	"go_server/game_server/api/service/bag"
	"go_server/game_server/api/service/build"
	"go_server/game_server/constants"
	"go_server/game_server/dao/scene_info_dao"
	"go_server/game_server/dataconfig/config_manager"
	"go_server/game_server/initializer/map_data_init"
	"go_server/game_server/protocol/pb"
	"go_server/game_server/util"
	"go_server/game_server/world_manager"
	"go_server/zinx/ziface"
	"go_server/zinx/znet"
)

type DreamSceneMopUpApi struct {
	znet.BaseRouter
}

func (*DreamSceneMopUpApi) Handle(request ziface.IRequest) {
	msg := &pb.DreamSceneMopUpC2S{}
	if !util.PBParseRet(request, msg) {
		return
	}

	conn := request.GetConnection()
	roleId := ServiceObj.GetRoleId(request)

	if msg.SceneId <= 0 {
		world_manager.WorldMgrObj.SendClientErrMsg(roleId, request, pb.ErrorCodeManager_PARAM_ERROR, nil)
		return
	}

	row := scene_info_dao.GetSceneByRoleId(roleId, []string{"scene_info"})
	if row == nil {
		world_manager.WorldMgrObj.SendClientErrMsg(roleId, request, pb.ErrorCodeManager_DREARM_SCENE_UNINIT, nil)
		return
	}

	tmpMap := make(map[int32][]int32)

	if row.SceneInfo.Data() != nil {
		tmpMap = row.SceneInfo.Data()
	}

	sceneInfo, ok := tmpMap[msg.SceneId]
	if !ok {
		world_manager.WorldMgrObj.SendClientErrMsg(roleId, request, pb.ErrorCodeManager_DREARM_SCENE_UNLOCK, nil)
		return
	}

	isFirst := sceneInfo[1] //是否已完成首通：0,未完成;1,已完成

	baseSceneInfo, ok := config_manager.GetStcTableLevelChapter_SheetLevelChapterById(msg.SceneId)

	if !ok {
		world_manager.WorldMgrObj.SendClientErrMsg(roleId, request, pb.ErrorCodeManager_DREARM_SCENE_UNFIND, nil)
		return
	}

	if isFirst <= 0 {
		world_manager.WorldMgrObj.SendClientErrMsg(roleId, request, pb.ErrorCodeManager_DREARM_SCENE_FAILED_COMPLETE_FIRST_BATTLE, nil)
		return
	}

	powerCost := baseSceneInfo.PowerCost         // 战斗消耗体力
	unlockBulidLv := baseSceneInfo.UnlockBulidLv // 当前关卡解锁建筑等级
	sweepReward := baseSceneInfo.SweepReward     // 扫荡奖励

	// 判断体力是否足够
	hadPowerNum := bag.GetItemNum(roleId, DREAM_SCENE_POWER_ID)
	if hadPowerNum < powerCost {
		world_manager.WorldMgrObj.SendClientErrMsg(roleId, request, pb.ErrorCodeManager_DREARM_SCENE_NOT_ENOUGH_POWER, nil)
		return
	}

	buildLevel := build.GetBuildLv(roleId, map_data_init.InitMapID, constants.DREAM_SCENE_BUILD_ID, constants.RESOURCE_BUILD_CFG_ID) // 获取梦境关卡建筑等级
	if buildLevel < unlockBulidLv {
		world_manager.WorldMgrObj.SendClientErrMsg(roleId, request, pb.ErrorCodeManager_DREARM_SCENE_NOT_ENOUGH_BUILDLEVEL, nil)
		return
	}

	prizeMap := make(map[int32]int32) // 战斗奖励

	// 扫荡奖励
	for _, v := range sweepReward {
		_, ok := prizeMap[v.Value[0]]
		if !ok {
			prizeMap[v.Value[0]] = v.Value[1]
		} else {
			prizeMap[v.Value[0]] += v.Value[1]
		}
	}

	// 组装返回数据结构
	res := new(pb.DreamSceneMopUpS2C)
	res.SceneId = msg.SceneId
	res.ItemMap = prizeMap
	res.PowerNum = hadPowerNum - powerCost

	conn.SendBuffPBMsg(uint32(pb.ProtocolManager_DreamSceneBattle), res)

	bag.SubItemSingle(roleId, DREAM_SCENE_POWER_ID, powerCost) // 扣除体力
	bag.AddItemBatch(roleId, prizeMap)                         // 发送战斗奖励
}
